// pages/rfid_binds/tray_search/tray_search.js
const mybehavior = require("../../../behaviors/requests")
Page({
  behaviors: [mybehavior],
  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    area: "全部",
    showPicker1: false,
    showPicker2: false,
    areas: ["全部", "托盘", "未绑定"],
    columns: [],
    stepper: 5,
    rfid: "",
    data: "",
    show_dialog: false,
    username: "",
    user_email: "",
    password: "",
    token: "",
    upFileList: [],
    fileList: [],
    list: [],
    loading: false,
    finished: true,
  },
  change(event) {
    let index = event.target.dataset.s;
    const self = this;
    let dynamicItem = self.data.form.dynamicItem;
    dynamicItem[index]["rfid"] = event.detail;
    self.setData({
      form: {
        dynamicItem: dynamicItem
      }
    })
  },
  setShowFalse() {
    this.setData({
      showPicker1: false
    })
  },
  setShowTrue() {
    this.setData({
      showPicker1: true
    })
  },
  setShow2False() {
    this.setData({
      showPicker2: false
    })
  },
  setShow2True() {
    this.setData({
      showPicker2: true
    })
  },
  scan(e) {
    let index = e.target.dataset.s;
    const self = this;
    wx.scanCode().then((res) => {
      const hardware_code = res.result;
      let dynamicItem = self.data.form.dynamicItem;
      dynamicItem[index]["rfid"] = hardware_code;
      self.setData({
        form: {
          dynamicItem: dynamicItem
        }
      })
      self.setData({
        material_order: hardware_code
      });
    });
  },
  afterRead(file) {
    if (
      file.file.type.indexOf("video") !== -1 ||
      file.file.type.indexOf("image") !== -1
    ) {
      Toast.success("开始上传！");
      file.status = "uploading";
      file.message = "上传中";

      request({
        method: "post",
        url: "/video_transfer/fs_upload",
        params: file,
      }).then((res) => {
        console.log(res);
        file.status = "done";
        file.message = "上传完成";
      });

      // 图片上传
      this.data.upFileList.push(file);
      console.log(this.data.fileList);
    } else {
      this.data.fileList.pop(); //单张上传
      Toast("只能上传视频/图片");
      console.log(this.data.fileList);
    }
  },
  before_close(action, done) {
    request({
        url: base_url + "/container_manage/login",
        method: "post",
        params: {
          user_email: this.data.user_email,
          password: this.data.password,
        },
      })
      .then((res) => {
        const data = res.data;
        const token = data["token"];
        this.data.token = token;
        localStorage.setItem("token", token);
        const username = data["username"];
        this.data.username = username;
        localStorage.setItem("username", username);
        localStorage.setItem("user_email", this.data.user_email);
        done();
      })
      .catch(() => {
        done(false);
      });
  },
  addItem() {
    let dynamicItem = this.data.form.dynamicItem;
    dynamicItem.push({
      rfid: "",
    });
    this.setData({
      form: {
        dynamicItem: dynamicItem
      }
    })
    console.log(this.data.form)
  },
  sure(form) {
    console.log(this.data.form.dynamicItem.length, "length");
    this.data.$refs[form].validate((valid) => {
      if (valid) {
        alert("submit!");
      } else {
        console.log("error submit!!");
        return false;
      }
    });
  },
  deleteItem(e) {
    let index = e.target.dataset.s;
    let dynamicItem = this.data.form.dynamicItem;
    dynamicItem.splice(index, 1);
    this.setData({
      form: {
        dynamicItem: dynamicItem
      }
    })

  },
  onChange(event) {
    this.setData({
      stepper: event.detail
    })
  },
  get_data() {
    let that = this;
    this.post("https://logisticsengineering.gree.com/rfid_api/rfid_manage/return_dic", {
      date: that.data.stepper,
    }).then(res => {
      if (res.code == 20000) {
        console.log(res.data)
        that.setData({
          columns: res.data.door_value
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch((e) => {
      wx.showToast({
        title: e.msg,
        icon: "none"
      })
    })
  },
  onConfirm1(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    // Toast(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      showPicker1: false,
      value: value
    })
  },
  onConfirm2(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    // Toast(`当前值：${value}, 当前索引：${index}`);
    console.log(value)
    this.setData({
      showPicker2: false,
      area: value
    })
  },
  onSubmit() {
    let that = this;
    console.log(that.data.stepper);
    this.post("https://logisticsengineering.gree.com/rfid_api/rfid_manage/access_door_search", {
      value: that.data.value,
      date: that.data.stepper,
      area: that.data.area
    }).then(res => {
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
        })
        that.setData({
          list: res.data.rfid_data
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch((e) => {
      wx.showToast({
        title: e.msg,
        icon: "none"
      })
    })
  },
  close_dialog(action, done) {
    return done();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.get_data()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})